package com.swpu.service_remind.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_remind.entity.vo.MedicineRemindVo;
import com.swpu.service_remind.service.MedicineRemindService;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-03-13
 */
@RestController
@RequestMapping("/service_remind/medicine-remind")
public class MedicineRemindController {
    @Autowired
    private MedicineRemindService medicineRemindService;
    @PostMapping("/saveMedicineRemindSetting")
    @Decrypt
    @Encrypt
    public R saveMedicineRemindSetting(@RequestBody ResultEncryption resultEncryption, MedicineRemindVo medicineRemindVo, HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        medicineRemindService.saveMedicineRemindSetting(openid,medicineRemindVo);
        return R.ok();
    }
    @GetMapping("getMedicineRemindData")
    public R getMedicineRemindData(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        List<MedicineRemindVo> remindVos = medicineRemindService.getMedicineRemindData(openid);
        return R.ok().data("medicineRemindList",remindVos);
    }
    @GetMapping("getMedicineRemindById/{id}")
    public R getMedicineRemindById(HttpServletRequest request,@PathVariable String id){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        MedicineRemindVo medicineRemindVo = medicineRemindService.getMedicineRemindById(openid, id);
        return  R.ok().data("medicineRemindVo",medicineRemindVo);
    }
    @PostMapping("updateMedicineRemindById")
    @Decrypt
    @Encrypt
    public R updateMedicineRemindById(@RequestBody ResultEncryption resultEncryption, MedicineRemindVo medicineRemindVo,HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        medicineRemindService.updateMedicineRemindById(openid,medicineRemindVo);
        return R.ok();
    }
    @DeleteMapping("deleteMedicineRemindById/{id}")
    public R deleteMedicineRemindById(HttpServletRequest request,@PathVariable(name = "id") String id){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        medicineRemindService.deleteMedicineRemindById(id);
        return R.ok();
    }


}

